package com.uptown.matrix.common.result;



import java.util.Optional;


public class WebResult<T> {
    private StatusCode status = StatusCode.NORMAL;
    private String errstr;
    private T result;


    public WebResult() {
    }

    public WebResult(T result, StatusCode status, String errstr) {
        this.result = result;
        this.status = status;
        this.errstr = errstr;
    }


    public T getResult() {
        return this.result;
    }

    public WebResult<T> setResult(T result) {
        this.result = result;
        return this;
    }

    public WebResult<T> setStatus(StatusCode status) {
        this.status = status;
        return this;
    }

    public String getErrstr() {
        return this.errstr;
    }

    public WebResult<T> setErrstr(String errstr) {
        this.errstr = errstr;
        return this;
    }


    public static <T> WebResult<T> newInstance() {
        return new WebResult<T>();
    }

    public static <T> WebResult<T> of(T result) {
        if (result instanceof Optional) {
            return of(result);
        }
        WebResult<T> r = newInstance();
        r.setResult(result);
        return r;
    }




    /**
     * @return ${return_type}
     * @throws
     * @description: 自定义异常返回对象
     * @author WangChengYu
     * @date 2020-02-06 23:09
     */
    public static <T> WebResult<T> fail(String errstr, StatusCode errCode, T t) {
        return new WebResult<>(t, errCode, errstr);
    }

    public static <T> WebResult<T> fail(StatusCode errCode, String errstr) {
        return new WebResult<>(null, errCode, errstr);
    }

    public static <T> WebResult<T> fail(String errstr) {
        return new WebResult<T>(null, StatusCode.ABNORMAL, errstr);
    }
}
